package com.webcash.CNBoard.Controller;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.SessionAttributes;

import com.webcash.CNBoard.Model.Board;
import com.webcash.CNBoard.Model.Department;
import com.webcash.CNBoard.Model.DeptBoard;
import com.webcash.CNBoard.Model.DeptComment;
import com.webcash.CNBoard.Model.Employee;
import com.webcash.CNBoard.Model.Login;
import com.webcash.CNBoard.Model.MyDepartment;
import com.webcash.CNBoard.Model.Team;
import com.webcash.CNBoard.Service.DepartmentService;
import com.webcash.CNBoard.Service.DepartmentTeamService;
import com.webcash.CNBoard.Service.EmployeeService;


@Controller
@RequestMapping("/dept")
@SessionAttributes({"myDepartment","login"})
public class DepartmentListController {
	@Autowired
	DepartmentService departmentService;
	@Autowired
	DepartmentTeamService departmentTeamService;
	@Autowired
	EmployeeService employeeService;
	@Autowired
	MyDepartment myDepartment;

	@RequestMapping
	public String action(@ModelAttribute Login login, Model model){
		List<Department> departmentlist = departmentService.getDepartmentList();
		model.addAttribute("departmentlist", departmentlist);
		
		return "department/deptMainPage";
	}
	
	@RequestMapping("/deptMain")
	//비동기적으로 선택한 deptNo 저장하기
	@ResponseBody
	public String acitonMain(@RequestParam int deptNo){
		myDepartment.setDeptNo(deptNo);
		return "";
	}
	@RequestMapping("/deptteamList")
	public String action2(@ModelAttribute Login login, Model model){
		int pageNum = 1;
		
		List<Team> teamList = departmentTeamService.getTeamListbyDeptNo(myDepartment.getDeptNo());
		List<DeptBoard> boardList=departmentService.getDepartmentBoardListByDepartmentNo(myDepartment.getDeptNo(), pageNum);
		Department dept= departmentService.getDepartmentByDeptNo(myDepartment.getDeptNo());
		
		
		model.addAttribute("boardList", boardList);
		model.addAttribute("teamList", teamList);
		model.addAttribute("dept", dept);
		return "department/deptTeamList";
	}
	
	@RequestMapping("/deptBoardList")
	public String deptBoardList(@ModelAttribute Login login, Model model,@RequestParam(required=false) String num){
		
		int count=departmentService.getDepartmentBoardCountByDeptNo(myDepartment.getDeptNo());
		int pageNum = 1;
		if(num==null)
			System.out.println("num is null");
		else{
			pageNum=Integer.parseInt(num);
		}
		
		List<DeptBoard> boardList=departmentService.getDepartmentBoardListByDepartmentNo(myDepartment.getDeptNo(), pageNum);
		model.addAttribute("boardList", boardList);
		model.addAttribute("boardCount", count);
		model.addAttribute("pageNum", pageNum);
		return "department/deptBoard";
	}
	
	@RequestMapping(value="/deptteamMemberlist/{teamNo}")
	public String deptTeamList(@PathVariable int teamNo, @ModelAttribute Login login, Model model, @ModelAttribute Team team){
		
		List<Employee> teamMember = employeeService.getTeamMemberListbydeptNoAndTeamNo(team);
		List<Employee> staff = new ArrayList<Employee>();				//사원
		List<Employee> supervisor = new ArrayList<Employee>();			//주임
		List<Employee> assistantManger= new ArrayList<Employee>();		//대리
		List<Employee> deputyManager = new ArrayList<Employee>();		//과장
		List<Employee> Manager = new ArrayList<Employee>();				//부장
		for(int i=0;i<teamMember.size();i++){
			switch(Integer.parseInt(teamMember.get(i).getPosition())){
			case 1:
				staff.add(teamMember.get(i));
				break;
			case 2:
				supervisor.add(teamMember.get(i));
				break;
			case 3:
				assistantManger.add(teamMember.get(i));
				break;
			case 4:
				deputyManager.add(teamMember.get(i));
				break;
			case 5:
				Manager.add(teamMember.get(i));
				break;
			}
			
		}
		model.addAttribute("staff", staff);
		model.addAttribute("supervisor", supervisor);
		model.addAttribute("assistantmanager", assistantManger);
		model.addAttribute("deputymanager", deputyManager);
		model.addAttribute("manager", Manager);
		
		return "department/deptTeamPage";
	}
	
	@RequestMapping("/write")
	public String write(Model model){
		
		return "department/deptBoardWrite";
	}
	
	@RequestMapping(value="/writeAction", method=RequestMethod.POST)
	@ResponseBody
	public String writeAction(@RequestParam String title, @RequestParam String article, 
			@ModelAttribute Login login, Model model, @ModelAttribute Board board){
		String data="fail";
		
		board.setTitle(title);
		board.setArticle(article);
		
		departmentService.addDeptBoardByEmpNo(login.getId(), board, myDepartment.getDeptNo());
		
		return data;
	}
	
	@RequestMapping(value="/contents/{boardNo}")
	public String boardContents(@PathVariable int boardNo,@ModelAttribute Login login, Model model, @RequestParam int num, @RequestParam(required=false) boolean updateComment){
		System.out.println(boardNo+"af" + updateComment);
		DeptBoard boardContents=departmentService.getDepartmentBoardContentsByBoardNo(boardNo);
		if(!updateComment)
			departmentService.updateDeptBoardForReadCountByDeptBoardNo(myDepartment.getDeptNo(), boardContents.getDeptBoardNo());
		
		List<DeptComment> commentList = departmentService.getDeptCommentListByBoardNo(boardNo);
		
		System.out.println(commentList);
		
		model.addAttribute("boardContents", boardContents);
		model.addAttribute("pageNum", num);
		model.addAttribute("commentList", commentList);
		return "department/deptBoardContents";
	}
	
	@RequestMapping("/modify")
	public String boardModify(@RequestParam int deptBoardNo, @ModelAttribute Login login, Model model){
		System.out.println("deptBoardNo"+deptBoardNo+" deptNo: "+myDepartment.getDeptNo());
		DeptBoard deptBoard = departmentService.getDepartmentBoardByDeptNoAndDeptBoardNo(myDepartment.getDeptNo(), deptBoardNo);
		System.out.println("deptModify  "+deptBoard);
		
		model.addAttribute("deptBoard", deptBoard);
		
		return "department/deptBoardModify";
	}
	
	@RequestMapping(value="/modifyAction")
	@ResponseBody
	public String modifyAction(@ModelAttribute Login login,@ModelAttribute Board board, Model model,
			@RequestParam int deptBoardNo, @RequestParam String title, @RequestParam String article){
		System.out.println("modifyAction");
		board.setTitle(title);
		board.setArticle(article);
		
		departmentService.updateDeptBoardByDeptBoardNo(board, deptBoardNo, myDepartment.getDeptNo());
		return "";
	}
	
	@RequestMapping(value="/delete/{deptBoardNo}")
	public String deleteAction(@PathVariable int deptBoardNo, @ModelAttribute Login login){
		
		departmentService.deleteDeptBoardByDeptNoAndDeptBoardNo(myDepartment.getDeptNo(), deptBoardNo);
		return "department/deptBoard";
	}
	
	@RequestMapping(value="/comment/write")
	@ResponseBody
	public String commentWrite(@ModelAttribute Login login,@RequestParam int boardNo, @RequestParam String commentContents,  Model model){
		System.out.println(commentContents +"/" + boardNo);
		
		departmentService.addDeptCommentByBoardNoAndId(boardNo, login.getId(), commentContents);
		
		return "";
	}

}
